﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;

namespace RaceList.Admin.Relay
{
    public partial class RelayList : System.Web.UI.Page
    {
        ipowdb_suibian_webDataContext db = ConnHelper.ipowdb_suibian_web();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FenYe();
            }
        }

        protected void gvList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int? ID = Convert.ToInt32(e.CommandArgument.ToString());
            Activity_AwardUser awardUser = db.Activity_AwardUser.SingleOrDefault(s => s.Id == ID);

            if (awardUser != null)
            {
                string action = string.Empty;
                try
                {
                    switch (e.CommandName)
                    {
                        case "CheckAward":
                            if (!awardUser.isCheck)
                            {
                                awardUser.isCheck = true;
                                awardUser.CheckDate = DateTime.Now;
                                db.SubmitChanges();
                            }
                            action = "修改";
                            break;
                    }

                    this.Message.Text = "操作成功";
                }
                catch (Exception ex)
                {
                    StringHelper.Alert(this, "操作失败！", string.Empty);
                    this.Message.Text = "操作失败，失败原因：" + ex.Message + "！详细原因请查看错误日志。";
                }
                FenYe();
            }
            else
            {
                StringHelper.Alert(this, "此用户不存在，请您刷新本页面后继续操作！", string.Empty);
            }
        }

        protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#EFF3FB'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='White'");
            }
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            FenYe();
        }

        #region 分页
        protected int GetCrrentPage()
        {
            int currentPage = 1;
            if (Request.Form["__EVENTARGUMENT"] != null)
            {
                Int32.TryParse(Request.Form["__EVENTARGUMENT"], out currentPage);
                currentPage = currentPage == 0 ? 1 : currentPage;
            }
            return currentPage;
        }
        private List<Activity_Relay> GetDataSource(bool firstLoad = false)
        {
            if (txtName.Text.Trim() != "")
            {
                return db.Activity_Relay
                    .Where(e => e.SumScore < 992)
                    .Where(e => e.WeixinUser.Nickname == txtName.Text.Trim())
                    .OrderByDescending(s => s.SumScore)
                    .Skip((Paging1.CurrentPage - 1) * Paging1.PageSize).Take(Paging1.PageSize).ToList();
            }
            else
            {
                return db.Activity_Relay.Where(e => e.SumScore < 992).OrderByDescending(s => s.SumScore).Skip((Paging1.CurrentPage - 1) * Paging1.PageSize).Take(Paging1.PageSize).ToList();
            }
        }
        protected object GetCurrentPageDataSource()
        {
            var list = GetDataSource();

            return list.Select(s => new
            {
                s.Id,
                s.SumScore,
                s.AddTime,
                s.EditTime,
                NickName = s.WeixinUser == null ? "" : s.WeixinUser.Nickname,
                Headimgurl = s.WeixinUser == null ? "" : s.WeixinUser.Headimgurl,

            });
        }

        protected int GetTotalNum()
        {
            if (txtName.Text.Trim() != "")
            {
                return db.Activity_Relay.Where(e => e.WeixinUser.Nickname == txtName.Text.Trim()).Count();
            }
            else
            {
                return db.Activity_Relay.Count();
            }
        }
        protected void FenYe()
        {
            Paging1.PageSize = 20;
            Paging1.CurrentPage = GetCrrentPage();
            Paging1.TotalNum = GetTotalNum();
            Paging1.Paging_Bind();
            gvList.DataSource = GetCurrentPageDataSource();
            gvList.DataBind();
        }
        protected void btnPage_Click(object sender, EventArgs e)
        {
            FenYe();
        }
        #endregion

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            FenYe();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            FenYe();
        }
    }
}